%Note: I=5e4 in run_aax_bunching_20210415.m
global option k_input
option = 'proportional'; % proportional | fixed
k_input = [0;0];
run 'run_aax_bunching_20210415_I.m'
save pre_regulation.mat  
k_input = [0.0032; 0.0009];
run 'run_aax_bunching_20210415_I.m'

save regulation.mat 
clearvars; 
clc; close all;

dt_pre = load('pre_regulation.mat');
dt_reg = load('regulation.mat');

dz = dt_pre.dz;
z = dt_pre.z;
beta = dt_pre.beta;
R_pre = dt_pre.R;
R_reg = dt_reg.R;
z_min = dt_pre.z_min;

Q_pre = exp(dt_pre.qstar);
Q_reg = exp(dt_reg.qstar);

%%
amin = 7;
amax = 13;
N = 200;
x1 = linspace(amin,amax,N)';
dx1 = x1(2) - x1(1);
dt_reg.f = dt_reg.f/sum(dt_reg.f(Q_reg>=amin & Q_reg<=amax)*dz);
dt_pre.f = dt_pre.f/sum(dt_pre.f(Q_pre>=amin & Q_pre<=amax)*dz);
f_pre = dt_pre.f*dz/dx1;
f_reg = dt_reg.f*dz/dx1;
[bch1,dist_pre1] = gp_dist(Q_pre,f_pre,x1);
[~,dist_reg1] = gp_dist(Q_reg,f_reg,x1);

%q2_start = 45;
%q2_end = 55;
%x2 = linspace(q2_start,q2_end,200)';
%[bch2,dist_pre2] = gp_dist(q_pre,f_pre,x2);
%[~,dist_reg2] = gp_dist(q_reg,f_reg,x2);

figure
hold on
plot(bch1,dist_pre1,'LineWidth',1.2)
plot(bch1,dist_reg1,'-.','LineWidth',1.2)
xlabel('Asset Size')
 set(gca, 'yScale', 'log')
  set(gca, 'xScale', 'log')
  xlim([amin amax])
legend('Without regulation','With regulation')
hold off
%figure
%plot(bch2,dist_pre2)
%hold on



figure
hold on
plot(bch1,dist_pre1,'LineWidth',1.2)
plot(bch1,dist_reg1,'-.','LineWidth',1.2)
xlabel('Asset Size')
 set(gca, 'yScale', 'log')
  set(gca, 'xScale', 'log')
  xlim([amin amax])
legend('Without regulation','With regulation')
hold off

%plot(bch2,dist_reg2)



%%
figure
hold on
plot(Q_pre,1-cumsum(f_pre)/sum(f_pre),'LineWidth',1.2)
plot(Q_reg,1-cumsum(f_reg)/sum(f_reg),'LineWidth',1.2)
xlabel('Asset Size')
 set(gca, 'yScale', 'log')
  set(gca, 'xScale', 'log')
  xlim([amin amax])
%   ylim([min(1-cumsum(f_reg)/sum(f_reg)),max(1-cumsum(f_reg)/sum(f_reg))])
legend('Without regulation','With regulation')
hold off
